from django.shortcuts import render
from .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.


class GetCarouser(APIView):
    def post(self, request):
        lbt = Carousel.objects.all()
        ser = CarouselSER(lbt, many=True)
        return Response({'carousel': ser.data})


class OneCategoryALL(APIView):
    def post(self, request):
        Categoryname = request.data.get('categoryName')
        shop_id = Category.objects.get(name=Categoryname)
        shop = shop_id.goods_set.all()
        res = GoodsSER(shop, many=True)
        return Response(res.data)


class GetHotProduct(APIView):
    def post(self, request):
        Categoryname = request.data.get('categoryName')
        list = []
        for i in Categoryname:
            shop_id = Category.objects.get(name=i)
            shop = shop_id.goods_set.all().order_by('selling_price')[0]
            list.append(shop)
        res = GoodsSER(list, many=True)
        return Response(res.data)


class OneGood(APIView):
    def post(self, request):
        id = request.data.get('productID')
        shop = Goods.objects.get(id=id)
        res = GoodsSER(shop)
        return Response(res.data)


class OneGoodImgS(APIView):
    def post(self, request):
        id = request.data.get('productID')
        shop_id = Goods.objects.get(id=id)
        data = shop_id.goodimg_set.all()
        res = GoodImgSER(data, many=True)
        return Response(res.data)


class GoodGetAllProduct(APIView):
    # len=0
    def post(self, request):
        categoryID = request.data.get('categoryID')
        currentPage = request.data.get('currentPage')
        pageSize = request.data.get('pageSize')
        shop = Goods.objects.all()
        res = GoodsSER(shop, many=True)
        return Response({'product': res.data, 'total': len(res.data)})


class GoodGetProductByCategory(APIView):
    # len!=0
    def post(self, request):
        categoryID = request.data.get('categoryID')[0]
        currentPage = request.data.get('currentPage')
        pageSize = request.data.get('pageSize')
        shop = Category.objects.get(id=categoryID)
        shop_id = shop.goods_set.all()
        res = GoodsSER(shop_id, many=True)
        return Response({'product': res.data, 'total': len(res.data)})


class CategoryGetCategory(APIView):
    def post(self, request):


        shop = Category.objects.all()
        res = CategorySER(shop, many=True)
        return Response({'category': res.data})
    
